-- 通用注释函数
local comment_line = function() return require("vim._comment").operator() .. "_" end
local comment_operator = function() return require("vim._comment").operator() end

return {
  "AstroNvim/astrocore",
  opts = {
    mappings = {
      i = {
        ["jk"] = { "<ESC>" },
        ["<C-h>"] = { "<Left>" },
        ["<C-j>"] = { "<Down>" },
        ["<C-k>"] = { "<Up>" },
        ["<C-l>"] = { "<Right>" },
        ["<C-a>"] = { "<Home>" },
        ["<C-e>"] = { "<End>" },
        ["<C-d>"] = { "<Delete>" },
      },

      n = {
        ["<leader>s"] = { "<cmd>w<cr>" },
        ["<Tab>"] = {
          function() require("astrocore.buffer").nav(vim.v.count1) end,
          desc = "Next buffer",
        },
        ["<S-Tab>"] = {
          function() require("astrocore.buffer").nav(-vim.v.count1) end,
          desc = "Previous buffer",
        },
        ["<leader>mp"] = { "<Plug>MarkdownPreview" },
        ["U"] = { "<C-u>" },
        ["D"] = { "<C-d>" },
        ["Y"] = { "y$" },
        ["L"] = { "$" },
        ["H"] = { "^" },
        [";"] = { ":" },
        ["j"] = { "gj" },
        ["k"] = { "gk" },
        ["<C-h>"] = { "<C-w><Left>" },
        ["<C-l>"] = { "<C-w><Right>" },
        ["<C-j>"] = { "<C-w><Down>" },
        ["<C-k>"] = { "<C-w><Up>" },
        ["ca"] = {
          function() vim.lsp.buf.code_action() end,
          desc = "LSP code action",
        },
        ["=="] = {
          function() vim.lsp.buf.format() end,
          desc = "Format file",
        },
        ["K"] = {
          function() vim.lsp.buf.hover() end,
          desc = "Show hover",
        },
        ["<leader>rn"] = {
          function() vim.lsp.buf.rename() end,
          desc = "Rename symbol",
        },
        ["<leader>wa"] = {
          function() vim.lsp.buf.add_workspace_folder() end,
          desc = "Add workspace folder",
        },
        ["<leader>wr"] = {
          function() vim.lsp.buf.remove_workspace_folder() end,
          desc = "Remove workspace folder",
        },
        ["<leader>wl"] = {
          function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end,
          desc = "List workspace folders",
        },
        -- 错误跳转映射
        ["ge"] = {
          function() vim.diagnostic.goto_next() end,
          desc = "Go to next error/warning",
        },
        ["gE"] = {
          function() vim.diagnostic.goto_prev() end,
          desc = "Go to previous error/warning",
        },
        ["ga"] = {
          "`.",
          desc = "Go to last change",
        },
        -- Flutter 命令映射
        ["<leader>dfr"] = {
          "<cmd>FlutterRun<cr>",
          desc = "Flutter Run",
        },
        ["<leader>dfh"] = {
          "<cmd>FlutterHotReload<cr>",
          desc = "Flutter Hot Reload",
        },
        ["<leader>dfR"] = {
          "<cmd>FlutterHotRestart<cr>",
          desc = "Flutter Hot Restart",
        },
        ["<leader>dfq"] = {
          "<cmd>FlutterQuit<cr>",
          desc = "Flutter Quit",
        },
        ["<leader>dfd"] = {
          "<cmd>FlutterDevices<cr>",
          desc = "Flutter Devices",
        },
        ["<leader>dft"] = {
          function()
            require("neotest").run.run()
            vim.defer_fn(function() require("neotest").summary.open() end, 100)
          end,
          desc = "Run the nearest test",
        },

        ["<leader>dfc"] = {
          function() require("telescope").extensions.flutter.commands() end,
          desc = "Show all commands of FlutterTools",
        },

        ["<leader>dfT"] = {
          function()
            require("neotest").run.run(vim.fn.expand "%")
            vim.defer_fn(function() require("neotest").summary.open() end, 100)
          end,
          desc = "Run test of current file",
        },
        -- DAP 调试映射
        ["<leader>dr"] = {
          function()
            local dapui = require "dapui"
            if dapui then
              dapui.toggle "repl"
            else
              vim.notify("DAP UI not available", vim.log.levels.WARN)
            end
          end,
          desc = "Toggle DAP REPL",
        },
        ["<leader>do"] = {
          function()
            local dapui = require "dapui"
            if dapui then
              dapui.open "repl"
            else
              vim.notify("DAP UI not available", vim.log.levels.WARN)
            end
          end,
          desc = "Open DAP REPL",
        },
        ["<leader>dc"] = {
          function()
            local dapui = require "dapui"
            if dapui then
              dapui.close "repl"
            else
              vim.notify("DAP UI not available", vim.log.levels.WARN)
            end
          end,
          desc = "Close DAP REPL",
        },

        ["<C-M-o>"] = {
          function()
            vim.lsp.buf.code_action {
              context = { only = { "source.organizeImports" } },
              apply = true,
            }
          end,
          desc = "Remove unused imports",
        },
        -- 分屏跳转映射 (类似IntelliJ IDEA的GoToTab1-9)
        ["<leader>1"] = {
          function()
            -- 跳转到第1个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 1 then
              vim.cmd("buffer " .. buflist[1].bufnr)
            else
              vim.notify("No buffers available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 1",
        },
        ["<leader>2"] = {
          function()
            -- 跳转到第2个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 2 then
              vim.cmd("buffer " .. buflist[2].bufnr)
            else
              vim.notify("Buffer 2 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 2",
        },
        ["<leader>3"] = {
          function()
            -- 跳转到第3个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 3 then
              vim.cmd("buffer " .. buflist[3].bufnr)
            else
              vim.notify("Buffer 3 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 3",
        },
        ["<leader>4"] = {
          function()
            -- 跳转到第4个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 4 then
              vim.cmd("buffer " .. buflist[4].bufnr)
            else
              vim.notify("Buffer 4 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 4",
        },
        ["<leader>5"] = {
          function()
            -- 跳转到第5个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 5 then
              vim.cmd("buffer " .. buflist[5].bufnr)
            else
              vim.notify("Buffer 5 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 5",
        },
        ["<leader>6"] = {
          function()
            -- 跳转到第6个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 6 then
              vim.cmd("buffer " .. buflist[6].bufnr)
            else
              vim.notify("Buffer 6 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 6",
        },
        ["<leader>7"] = {
          function()
            -- 跳转到第7个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 7 then
              vim.cmd("buffer " .. buflist[7].bufnr)
            else
              vim.notify("Buffer 7 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 7",
        },
        ["<leader>8"] = {
          function()
            -- 跳转到第8个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 8 then
              vim.cmd("buffer " .. buflist[8].bufnr)
            else
              vim.notify("Buffer 8 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 8",
        },
        ["<leader>9"] = {
          function()
            -- 跳转到第9个buffer
            local buflist = vim.fn.getbufinfo { buflisted = 1 }
            if #buflist >= 9 then
              vim.cmd("buffer " .. buflist[9].bufnr)
            else
              vim.notify("Buffer 9 not available", vim.log.levels.WARN)
            end
          end,
          desc = "Go to buffer 9",
        },
        -- 注释映射
        ["cc"] = {
          function() vim.fn.feedkeys(comment_line(), "n") end,
          desc = "Toggle comment line using Neovim's built-in comment",
        },
      },
      v = {
        ["L"] = { "$" },
        ["H"] = { "^" },
        ["J"] = { ":m '>+1<CR>gv=gv" },
        ["K"] = { ":m '<-2<CR>gv=gv" },
        -- 注释映射 (visual模式)
        ["cc"] = {
          function() vim.fn.feedkeys(comment_operator(), "n") end,
          desc = "Toggle comment selection using Neovim's built-in comment",
        },
      },
    },
  },
}
